﻿package com.site.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.common.BaseController;
import com.entity.Production;
import com.service.ContactItemsService;
import com.service.ProductCategoryService;
import com.service.ProductionService;

@Controller
@RequestMapping("/")
public class CategoryController extends BaseController {
	@Autowired
	private ProductCategoryService productCategoryService;
	@Autowired
	private ProductionService productionService;
	@Autowired
	private ContactItemsService contactItemsService;
	@RequestMapping("category/cate-{id}/p{pageNo}")
	public String index(HttpServletRequest request, @PathVariable int id, @PathVariable Integer pageNo){
		this.initCommon(request, productCategoryService, contactItemsService, productionService);
		System.out.println("请求第" + pageNo + "页");
		if(pageNo == null || pageNo < 1){
			pageNo = 1;
		}
		int total = this.productionService.countByCategory(id);
		int pageNum = (total-1)/6+1;
		if(pageNo > pageNum){
			pageNo = pageNum;
		}
		List<Production> page = this.productionService.findByCategoryInPage(id, 6*(pageNo-1), 6);
		request.setAttribute("page", page);
		request.setAttribute("categoryName", this.productCategoryService.findById(id).getCategoryName());
		request.setAttribute("total", total);
		request.setAttribute("pageNo", pageNo);
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("id", id);
		return "site.category";
	}
}
